From 637350e4d152de1346857d645bda8443900ec6f4 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Mon, 14 Feb 2022 13:57:06 +0100
Subject: fix: update comments list when a new comment is send

The comments list was static before. If an user posted a comment, even
after it was approved, the comments list was keeping the old state.
---
 src/pages/article/[slug].tsx | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

(limited to 'src/pages/article/[slug].tsx')

diff --git a/src/pages/article/[slug].tsx b/src/pages/article/[slug].tsx
index 7bde448..a0255fa 100644
--- a/src/pages/article/[slug].tsx
+++ b/src/pages/article/[slug].tsx
@@ -6,7 +6,11 @@ import PostHeader from '@components/PostHeader/PostHeader';
 import Sidebar from '@components/Sidebar/Sidebar';
 import Spinner from '@components/Spinner/Spinner';
 import { Sharing, ToC } from '@components/Widgets';
-import { getAllPostsSlug, getPostBySlug } from '@services/graphql/queries';
+import {
+  getAllPostsSlug,
+  getCommentsByPostId,
+  getPostBySlug,
+} from '@services/graphql/queries';
 import styles from '@styles/pages/Page.module.scss';
 import { NextPageWithLayout } from '@ts/types/app';
 import { ArticleMeta, ArticleProps } from '@ts/types/articles';
@@ -25,7 +29,10 @@ import { useIntl } from 'react-intl';
 import { Blog, BlogPosting, Graph, WebPage } from 'schema-dts';
 import '@utils/plugins/prism-color-scheme';
 
-const SingleArticle: NextPageWithLayout<ArticleProps> = ({ post }) => {
+const SingleArticle: NextPageWithLayout<ArticleProps> = ({
+  comments,
+  post,
+}) => {
   const intl = useIntl();
   const router = useRouter();
 
@@ -47,7 +54,7 @@ const SingleArticle: NextPageWithLayout<ArticleProps> = ({ post }) => {
 
   const {
     author,
-    comments,
+    commentCount,
     content,
     databaseId,
     dates,
@@ -62,7 +69,7 @@ const SingleArticle: NextPageWithLayout<ArticleProps> = ({ post }) => {
 
   const meta: ArticleMeta = {
     author,
-    commentCount: comments.length,
+    commentCount: commentCount || undefined,
     dates,
     readingTime: info.readingTime,
     thematics,
@@ -105,7 +112,7 @@ const SingleArticle: NextPageWithLayout<ArticleProps> = ({ post }) => {
     description: intro,
     articleBody: content,
     author: { '@id': `${settings.url}/#branding` },
-    commentCount: comments.length,
+    commentCount: commentCount || undefined,
     copyrightYear: publicationDate.getFullYear(),
     creator: { '@id': `${settings.url}/#branding` },
     dateCreated: publicationDate.toISOString(),
@@ -220,11 +227,13 @@ export const getStaticProps: GetStaticProps = async (
   const translation = await loadTranslation(locale);
   const { slug } = context.params as PostParams;
   const post = await getPostBySlug(slug);
+  const comments = await getCommentsByPostId(post.databaseId);
   const breadcrumbTitle = post.title;
 
   return {
     props: {
       breadcrumbTitle,
+      comments,
       post,
       translation,
     },
-- 
cgit v1.2.3

